﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using WqLunTan.Filters;
using Microsoft.AspNetCore.Http;

namespace WqLunTan.Controllers
{
    /// <summary>
    /// 前台用户控制器
    /// </summary>
    [Auth]
    public class AccountController : BaseController
    {
        /// <summary>
        /// 设置昵称
        /// </summary>
        /// <returns></returns>
        public IActionResult Setting()
        {
            return View();
        }

        /// <summary>
        /// 更新用户资料
        /// </summary>
        /// <param name="nick_name"></param>
        /// <returns></returns>
        public IActionResult UpdateUser(string nick_name)
        {
            if (!string.IsNullOrWhiteSpace(nick_name) && nick_name.Length >= 2)
            {
                var userCount = DbContext.Users.Where(x => x.NickName.Equals(nick_name)).Count();
                if (userCount > 0)
                {
                    return JsonFailResult("昵称已存在,请输入其他昵称");
                }
                var user = DbContext.Users.Where(x => x.Id == LoginUser.Id).SingleOrDefault();
                if (user != null)
                {
                    user.NickName = nick_name;
                    DbContext.SaveChanges();

                    user.LoginTime = DateTimeOffset.UtcNow;
                    HttpContext.Session.setValue("LoginUser", user);
                }
            }
            return JsonOkResult("修改成功");
        }
    }
}